//
//  CurrentViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/17.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "CurrentViewController.h"
#import "ProjectIntroductionViewController.h"
#import "CapitalSubsidiaryViewController.h"
#import "UIScrollView+UITouch.h"
#import "PayMoneyViewController.h"
#import "WelcomeLoginViewController.h"

@interface CurrentViewController ()<UITextFieldDelegate, UIAlertViewDelegate>
{
    UIView *_footView;
    
    NSString *_str;
    NSString *_smount;
    
}
@property (weak, nonatomic) IBOutlet UIProgressView *peogressView;
@property (weak, nonatomic) IBOutlet UILabel *progressLB;
@property (weak, nonatomic) IBOutlet UIButton *diya;

@property (weak, nonatomic) IBOutlet UITextField *moneyTextField;
@property (nonatomic, strong)  NSMutableArray *borrowArr;//用来存储明细数据
@property (weak, nonatomic) IBOutlet UILabel *ceshi;
@property (nonatomic, strong)  NSMutableArray *borrowList;//用来存储明细数据
@end

#define margin 10

@implementation CurrentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _totalMoney.delegate = self;
    _str = [NSString string];


    self.navigationItem.title = self.item;

    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:NO];
    } right:nil rightClick:nil];
}

-(NSMutableArray *)borrowArr{
    if (!_borrowArr) {
        _borrowArr = [NSMutableArray array];
    }
    return _borrowArr;
}

-(NSMutableArray *)borrowList{
    if (!_borrowList) {
        _borrowList = [NSMutableArray array];
    }
    return _borrowList;
}




//项目介绍
- (IBAction)introduce:(id)sender {
    
    CapitalSubsidiaryViewController *capitalView = [[CapitalSubsidiaryViewController alloc] init];
    BorrowGetDetailModel *model = [self.borrowArr firstObject];
    capitalView.Borrowid = model.BorrowId;
    capitalView.num = 2;
    capitalView.Code = model.Code;
    capitalView.ProjectName = model.ProjectName;
    [self setHidesBottomBarWhenPushed:YES];
    [capitalView setSelectIndex: 0];
    [self.navigationController pushViewController:capitalView animated:YES];
    
}
//投资记录
- (IBAction)record:(id)sender {
    
    CapitalSubsidiaryViewController *capitalView = [[CapitalSubsidiaryViewController alloc] init];
    BorrowGetDetailModel *model = [self.borrowArr firstObject];
    capitalView.Borrowid = model.BorrowId;
    capitalView.num = 2;
    capitalView.Code = model.Code;
    capitalView.ProjectName = model.ProjectName;
    [capitalView setSelectIndex: 1];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:capitalView animated:YES];
}
//投资协议
- (IBAction)plan:(id)sender {
    
    CapitalSubsidiaryViewController *capitalView = [[CapitalSubsidiaryViewController alloc] init];
    BorrowGetDetailModel *model = [self.borrowArr firstObject];
    capitalView.Borrowid = model.BorrowId;
    [self setHidesBottomBarWhenPushed:YES];
    capitalView.num = 2;
    capitalView.Code = model.Code;
    capitalView.ProjectName = model.ProjectName;
    [capitalView setSelectIndex: 2];
    [self.navigationController pushViewController:capitalView animated:YES];
}

//立即投资
- (IBAction)immediateInvestment:(UIButton *)sender {
    
    if (self.borrowArr.count) {
        BorrowGetDetailModel *model = [self.borrowArr firstObject];
        NSString *smount = [NSString stringWithFormat:@"%.2f",model.SurplueAmount];
        _smount = smount;
    }
   
   
    
    if ([self.moneyTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入投资金额" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    NSString *string = _moneyTextField.text;
    NSRange range = [string rangeOfString:@"0"];
    if (range.location == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"输入格式有误" message:nil delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    else if ([_smount isEqualToString:@"0.00"] ){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已满标" message:nil delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;

    }/*else if([[[NSUserDefaults standardUserDefaults] objectForKey:@"Blance"] integerValue]  > [self.moneyTextField.text integerValue]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"余额不足,请充值" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;

    }*/else if ([_moneyTextField.text integerValue] > [_smount integerValue]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"投资金额已超过剩余可投资金额" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    else {
    
    
    PayMoneyViewController *payMoneyVC = [[PayMoneyViewController alloc] init];
    payMoneyVC.money = self.moneyTextField.text;
    
    if (self.borrowArr.count) {
        payMoneyVC.model = [self.borrowArr firstObject];
    }
    
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:payMoneyVC animated:YES];
           }
    [self.moneyTextField resignFirstResponder];
    
}



- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1 && alertView.tag == 10000) {
        WelcomeLoginViewController *loginView = [[WelcomeLoginViewController alloc] init];
        loginView.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginView animated:NO];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未登录，请先登录" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 10000;
        [alert show];
        return;
    }
    [self BorrowGetDetail];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    
}



//定期明细接口
-(void)BorrowGetDetail{
    
    NSDictionary *para = @{@"BorrowId":self.borrowId};
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Borrow.GetDetail" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        
        NSLog(@"%@",object);
        if ([object[@"Code"] isEqualToString:@"00000"]) {
            NSDictionary *dict = [object[@"Data"] firstObject];
            BorrowGetDetailModel *model = [BorrowGetDetailModel mj_objectWithKeyValues:dict];
            [self.borrowArr addObject:model];
            
            [self setDataWithModel:model];
            
            // 传值
            for (NSDictionary *dic in dict[@"List"]) {
                BorrowListArr *listModel = [BorrowListArr mj_objectWithKeyValues:dic];
                [self.borrowList addObject:listModel];
            }

        }

        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        
    } progress:nil];
    
}

//取值
- (void)setDataWithModel:(BorrowGetDetailModel *)model{
    NSString *str = @"%";
    _Rote.text = [NSString stringWithFormat:@"%.2f",model.Rate];
    _TotalAmount.text = [NSString stringWithFormat:@"%.2f",model.TotalAmount];
    _Term.text = model.Term;
    _Income.text = [NSString stringWithFormat:@"%.2f",model.Income];
    _surplueAmount.text = [NSString stringWithFormat:@"剩余可投资%.2f元",model.SurplueAmount];
    _minAmount.text = [NSString stringWithFormat:@"%.2f",model.MinAmount];
    _progressLB.text = [NSString stringWithFormat:@"已投资%.2f%@",[self.percentStr doubleValue],str];
    [_peogressView setProgress:[self.percentStr floatValue]/100];
    
    _ceshi.text = [NSString stringWithFormat:@"%@",model.UntiDetail];
    
    
    if ([self.num isEqualToString:@"123"]) {
        _diya.hidden = YES;
    }

    
    
}




@end
